package com.dqx.opengl_es_demo.face;

import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.view.Surface;

/**
 * OpenGL-ES图形渲染的工具类
 */
public class EGLUtils {

    private static final int EGL_RECORDABLE_ANDROID = 0x3142;
    //存储图像的内存区域，实际就是FrameBuffer
    private EGLSurface eglSurface = EGL14.EGL_NO_SURFACE;
    //存储OpenGL-ES绘图的状态信息
    private EGLContext eglCtx = EGL14.EGL_NO_CONTEXT;
    //显示设备
    private EGLDisplay eglDis = EGL14.EGL_NO_DISPLAY;

    public void initEGL(Surface surface) {
        //获取显示设备
        eglDis = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
        //EGL版本信息
        int[] version = new int[2];
        //初始化EGL
        EGL14.eglInitialize(eglDis, version, 0, version, 1);
        //FrameBuffer的配置属性
        int confAttr[] = {
                EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT,
                EGL14.EGL_RED_SIZE, 8,
                EGL14.EGL_GREEN_SIZE, 8,
                EGL14.EGL_BLUE_SIZE, 8,
                EGL14.EGL_ALPHA_SIZE, 8,
                EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
                EGL_RECORDABLE_ANDROID, 1,
                EGL14.EGL_SAMPLE_BUFFERS, 1,
                EGL14.EGL_SAMPLES, 4,
                EGL14.EGL_NONE
        };
        //FrameBuffer的参数
        EGLConfig[] configs = new EGLConfig[1];
        int[] numConfigs = new int[1];
        //选择配置参数
        EGL14.eglChooseConfig(eglDis, confAttr, 0, configs, 0, 1, numConfigs, 0);
        int ctxAttr[] = {
                EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,// 0x3098
                EGL14.EGL_NONE
        };
        //创建Context
        eglCtx = EGL14.eglCreateContext(eglDis, configs[0], EGL14.EGL_NO_CONTEXT, ctxAttr, 0);
        int[] surfaceAttr = {
                EGL14.EGL_NONE
        };
        //创建可实际显示的Surface
        eglSurface = EGL14.eglCreateWindowSurface(eglDis, configs[0], surface, surfaceAttr, 0);
        //将EGLSurface，EGLContext和EGLDisplay绑定起来
        EGL14.eglMakeCurrent(eglDis, eglSurface, eglSurface, eglCtx);
    }

    /**
     * 获取EGLContext对象
     *
     * @return
     */
    public EGLContext getContext() {
        return eglCtx;
    }

    /**
     * 绘制并显示一帧图形
     */
    public void swap() {
        EGL14.eglSwapBuffers(eglDis, eglSurface);
    }

    /**
     * 释放资源
     */
    public void release() {
        if (eglSurface != EGL14.EGL_NO_SURFACE) {
            EGL14.eglMakeCurrent(eglDis, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
            EGL14.eglDestroySurface(eglDis, eglSurface);
            eglSurface = EGL14.EGL_NO_SURFACE;
        }
        if (eglCtx != EGL14.EGL_NO_CONTEXT) {
            EGL14.eglDestroyContext(eglDis, eglCtx);
            eglCtx = EGL14.EGL_NO_CONTEXT;
        }
        if (eglDis != EGL14.EGL_NO_DISPLAY) {
            EGL14.eglTerminate(eglDis);
            eglDis = EGL14.EGL_NO_DISPLAY;
        }
    }
}
